package cn.sxt.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.sxt.logistics.pojo.MsgObject;
import cn.sxt.logistics.pojo.Role;
import cn.sxt.logistics.pojo.RoleExample;
import cn.sxt.logistics.pojo.RoleExample.Criteria;
import cn.sxt.logistics.pojo.User;
import cn.sxt.logistics.pojo.UserExample;
import cn.sxt.logistics.service.RoleService;
import cn.sxt.logistics.service.UserService;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private UserService userService;

	@RequestMapping("/rolePage")
	@RequiresPermissions("role:rolePage")
	public String rolePage() {

		return "rolePage";
	}

	// 编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m, Long roleId) {

		// 修改操作
		if (roleId != null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改的回显数据
			Role role = roleService.selectByPrimaryKey(roleId);
			m.addAttribute("role", role);
		}

		return "roleEdit";
	}
	
	@RequiresPermissions("role:list")
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Role> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {
		System.out.println("keyword :" + keyword);
		// 开始分页
		PageHelper.startPage(pageNum, pageSize);

		RoleExample example = new RoleExample();

		// 关键字不为空才做条件查询
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
			criteria1.andRolenameLike("%" + keyword + "%");

		}

		List<Role> roles = roleService.selectByExample(example);

		// 创建分页信息对象
		return new PageInfo<Role>(roles);

	}

	// 删除功能
	@RequestMapping("/insert")
	@ResponseBody
	public MsgObject insert(Role role) {

		int row = roleService.insert(role);
		MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "新增数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/update")
	@ResponseBody
	public MsgObject update(Role role) {

		int row = roleService.updateByPrimaryKeySelective(role);
		MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "修改数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long roleId) {
		
		/*
		 * 删除前根据删除的角色id去管理员表查询是否还有管理员拥有这个角色
		 * 
		 * 有：不能删除
		 * 没有:可以删除
		 */
		
		UserExample example = new UserExample();
		cn.sxt.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<User> users = userService.selectByExample(example);
		
		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
		if(users.size() > 0) {
			mo = MsgObject.createMO(0, "此角色还拥有管理员，不能删除！");
			return mo;
		}
		


		int row = roleService.deleteByPrimaryKey(roleId);
		if (row == 1) {
			mo = MsgObject.createMO(1, "删除数据成功!");
		}
		return mo;
	}

}
